import java.util.Arrays;
import java.util.Stack;

public class Sort {
    //直接插入排序    数字越有序速度越快
    public static void insertSort(int[] arr){
        for(int i = 1; i < arr.length; i++){
            int tmp = arr[i];
            int j = i-1;
            for(; j >= 0; j--){
                if(arr[j] > tmp){
                    arr[j+1] = arr[j];
                } else{
                    break;
                }
            }
            arr[j+1] = tmp;
        }
    }
    //希尔排序  对直接插入排序的优化，缩小增量排序
    public static void shellSort(int[] arr){
        int gap = arr.length;
        while(gap > 1){
            gap /= 2;
            //进行增量为gap的直接插入排序
            for(int i = gap; i < arr.length; i++){
                int tmp = arr[i];
                int j = i-gap;
                for(; j >= 0; j -= gap){
                    if(arr[j] > tmp){
                        arr[j+gap] = arr[j];
                    } else {
                        break;
                    }
                }
                arr[j+gap] = tmp;
            }
        }
    }

    //两种选择排序时间复杂度相同
    //直接选择排序
    public static void selectSort(int[] arr){
        for(int i = 0; i < arr.length; i++){
            int minIndex = i;
            for(int j = i+1; j < arr.length; j++){
                if(arr[j] < arr[minIndex]){
                    minIndex = j;
                }
            }
            int tmp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = tmp;
        }
    }

    //两端选择排序
    public static void twoWaySelectSort(int[] arr){
        int left = 0;
        int right = arr.length - 1;
        while(left < right){
            int minIndex = left;
            int maxIndex = right;
            for(int i = left+1; i < right; i++){
                if(arr[i] < arr[minIndex]){
                    minIndex = i;
                }
                if(arr[i] > arr[maxIndex]){
                    maxIndex = i;
                }
            }
            swap(maxIndex,right,arr);
            swap(minIndex,left,arr);
            left++;
            right--;
        }
    }

    //堆排序
    public static void heapSort(int[] arr){
        createBigHeap(arr);
        int end = arr.length-1;
        for(int i = end; i > 0; i--){
            swap(i,0,arr);
            siftDown(arr,0,i);
        }
    }
    public static void createBigHeap(int[] arr){
        for(int parent = (arr.length-1-1)/2; parent >= 0; parent--){
            siftDown(arr,parent,arr.length);
        }
    }
    public static void siftDown(int[] arr, int parent, int end){
        int child = parent * 2 + 1;
        while(child < end){
            //令child指向左右孩子中最大的
            if(child+1 < end && arr[child] < arr[child+1]){
                child++;
            }
            if(arr[parent] < arr[child]){
                swap(parent,child,arr);
                parent = child;
                child = parent*2 + 1;
            } else {
                break;
            }
        }
    }
    //冒泡排序
    public static void bubbleSort(int[] arr){
        boolean flg = false;
        for(int i = 0; i < arr.length-1; i++){
            for(int j = 0; j <arr.length-1-i; j++){
                if(arr[j] > arr[j+1]){
                    swap(j,j+1,arr);
                    //若在一趟排序中没有发生交换，则已经有序可以直接退出
                    flg = true;
                }
            }
            if(flg == false){
                return;
            }
        }
    }

    //快速排序
    public static void quickSort(int[] arr){
        quick(arr,0,arr.length -1);
    }
    private static void quick(int[] arr, int start, int end){
        if(start >= end) return;
        //优化    三数取中
        int index = midOfThree(arr, start, end);
        swap(start,index,arr);  //交换后能保证start下标的数是三个数中最大的
        int partition = partition(arr, start, end);
        quick(arr,start,partition-1);
        quick(arr,partition+1,end);
    }
    //Hoare
    private static int partition(int[] arr,int left, int right){
        int i = left;
        int key = arr[left];
        while(left < right){
            while(left < right && key <= arr[right]){
                right--;
            }
            while(left< right && key >= arr[left]){
                left++;
            }
            swap(left, right, arr);
        }
        swap(left, i, arr);
        return left;
    }

    //挖坑法
    public static int partitionDig(int[] arr, int left, int right){
        int key = arr[left];
        while(left < right){
            while(left < right && arr[right] >= key){   //先走right则最后指向的是比key小的元素
                right--;
            }
            arr[left] = arr[right];
            while(left < right && arr[left] <= key){
                left++;
            }
            arr[right] = arr[left];
        }
        arr[left] = key;
        return left;
    }

    //三数取中
    private static int midOfThree(int[] arr, int left, int right){
        int mid = (left + right) / 2;
        if(arr[left] < arr[right]){
            if(arr[mid] < arr[left]){
                return left;
            } else if (arr[mid] > arr[right]){
                return right;
            } else{
                return mid;
            }
        }else {
            if(arr[mid] > arr[right]){
                return right;
            } else if(arr[mid] < arr[left]){
                return left;
            } else {
                return mid;
            }
        }
    }

    //快速排序非递归实现
    public static void quickSortNor(int[] arr){
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = arr.length - 1;
        int provit = partition(arr, left, right);
        if(provit - left > 1){
            stack.push(left);
            stack.push(provit - 1);
        }
        if(right - provit > 1){
            stack.push(provit + 1);
            stack.push(right);
        }
        while(!stack.isEmpty()){
            left = stack.pop();
            right = stack.pop();
            provit = partition(arr, left, right);
            if(provit - left > 1){
                stack.push(left);
                stack.push(provit - 1);
            }
            if(right - provit > 1){
                stack.push(provit + 1);
                stack.push(right);
            }
        }
    }
    //归并排序
    public static void mergeSortFunc(int[] arr){
        merge(arr, 0, arr.length - 1);
    }
    private static void merge(int[] arr, int left, int right){
        if(left >= right) return;
        //拆分
        int mid = (left + right) / 2;
        merge(arr, left, mid);
        merge(arr, mid+1, right);
        //合并
        combain(arr,left,right,mid);
    }

    //非递归实现
    public static void mergeSortNor(int[] arr){
        int gap = 1;
        while(gap < arr.length){
            //与递归实现不同，递归先分组再合并，非递归直接从单个为一组开始进行合并
            for(int i = 0; i < arr.length; i += 2*gap){
                int left = i;
                int mid = left + gap - 1;
                int right = mid + gap;
                if(mid >= arr.length){
                    mid = arr.length - 1;
                }
                if(right >= arr.length){
                    right = arr.length - 1;
                }
                combain(arr, left, right, mid);
            }
            gap *= 2;
        }
    }

    private static void combain(int[] arr, int left, int right, int mid) {
        int s1 = left;
        int s2 = mid + 1;
        int[] tmp = new int[right - left + 1];
        int k = 0;
        while(s1 <= mid && s2 <= right){
            if(arr[s2] > arr[s1]){
                tmp[k++] = arr[s1++];
            } else {
                tmp[k++] = arr[s2++];
            }
        }
        while(s1 <= mid){
            tmp[k++] = arr[s1++];
        }
        while(s2 <= right){
            tmp[k++] = arr[s2++];
        }
        //将tmp数据拷到arr当中
        for(int i = 0; i < tmp.length; i++){
            arr[i + left] = tmp[i];
        }
    }

    public static void count(int[] arr){
        //求最大值和最小值开辟数组长度
        int minVal = arr[0];
        int maxVal = arr[0];
        for(int i = 0; i < arr.length; i++){
            if(arr[i] < minVal){
                minVal = arr[i];
            }
            if(arr[i] > maxVal){
                maxVal = arr[i];
            }
        }
        int[] count = new int[maxVal - minVal + 1];
        //开始计数
        for(int i = 0; i < arr.length; i++){
            count[arr[i] - minVal]++;
        }
        //输出对应计数点位的数据
        int index = 0;
        for(int i = 0; i < count.length; i++){
            while (count[i]-- > 0){
                arr[index++] = i + minVal;
            }
        }
        //System.out.println(Arrays.toString(arr));
    }


    public static void swap(int i, int j, int[] arr){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
    public static void main(String[] args) {
        int[] arr = {6,9,2,3,5,10};
        //insertSort(arr);
        //shellSort(arr);
        //selectSort(arr);
        //twoWaySelectSort(arr);
        //heapSort(arr);
        //bubbleSort(arr);
        //quickSort(arr);
        //quickSortNor(arr);
        //mergeSortFunc(arr);
//        mergeSortNor(arr);
//        System.out.println("======");
        count(arr);
    }
}
